<%@ page import="java.util.ArrayList"%>
<%@ page import="com.alpha.bom.*" %>
<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
    pageEncoding="ISO-8859-1"%>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
</head>
<body>
	<c:if test="${sessionScope.user.userType ne 'Professor'}" >
		<c:redirect url="LoginServlet"></c:redirect>
	</c:if>
	<c:set var="categories" value="${applicationScope.categories}"/>
	<c:set var="message" value="${param.message}"></c:set>
	<c:set var="error" value="${param.error}"></c:set>
	<c:set var="success" value="${param.success}"></c:set>
	<c:set var="user" value="${sessionScope.user}"></c:set>

	<div id="header"><%@ include file="Header.jsp"%></div>
	<div id="content">
	<div id="navbar"><%@ include file="NavigationBar.jsp"%></div>
	
	<div id="main">
	
		<div style="" class="box">
			<div class="box-header well">
				<h2><i class="icon-edit"></i> Create Exam</h2>
				<div class="box-icon">
					<a href="#" class="btn btn-minimize btn-round"><i class="icon-chevron-up"></i></a>
				</div>
			</div>
			
			<div class="box-content">
				<form class="form-horizontal" action="CreateExamServlet" method="post">
					<fieldset>
						<div class="control-group">
							<label class="control-label" for="focusedInput">*Exam Name</label>
							<div class="controls">
								<input name="examName" class="input-xlarge focused" type="text">
							</div>
						</div>
						<div class="control-group">
							<label class="control-label" for="selectType">Exam Type</label>
							<div class="controls">
								<select  style="width: 280px" name="examType" id="selectType" data-rel="chosen">
									<option value="RealExam">Exam</option>
									<option value="PracticeExam">Practice Exam</option>
								</select>
							</div>
						</div>
						<div class="control-group">
							<label class="control-label" for="selectCategory">Exam Category</label>
							<div class="controls">
								<select style="width: 280px" name="examCategory" id="selectCategory" data-rel="chosen">
									<c:forEach var="catName" items="${categories}">
										<option value="${catName}">${catName}</option>
									</c:forEach>
								</select>
							</div>
						</div>
						<div class="form-actions">
							<button type="submit" class="btn btn-primary" name="createExam">Create Exam</button>
							<button class="btn" type="reset">Reset</button>
						</div>
					</fieldset>	  
				</form>   
			</div>
		</div>
				
				<div class="box">
					<div class="box-header well">
						<h2><i class="icon-list-alt"></i> View Exams</h2>
						<div class="box-icon">
							<a href="#" class="btn btn-minimize btn-round"><i class="icon-chevron-up"></i></a>
						</div>
					</div>
					<div class="box-content">
						<table class="table table-striped table-bordered bootstrap-datatable datatable">
							<thead>
								<tr>
									<th>Exam Name</th>
									<th>Exam Type</th>
									<th>Exam Category</th>
									<th>Action</th>
									<th>Activate/Deactivate</th>
								</tr>
							</thead>   
							<tbody>
								<c:forEach var="exam" items="${user.getExams()}">
									<tr>
									<td>${exam.examName}</td>
									<td>${exam.examType}</td>
									<td>${exam.category}</td>
									<td>
										<c:choose>
											<c:when test="${exam.getQuestions().size() gt 0}">
												<c:choose>
													<c:when test="${exam.examType eq 'Practice' or exam.examType eq 'Real' and exam.isLocked()}">
														<i class=" icon-ban-circle"></i> N/A
													</c:when>
													<c:when test="${exam.examType eq 'Real' and not exam.isLocked()}">
														<a class="btn btn-info" href="OngoingExamViewServlet?exam_id=${exam.examId}"><i class="icon-search"></i> View Exam Status</a>
													</c:when>
												</c:choose>
											</c:when>
											<c:otherwise>
												<a class="btn btn-success" href="AddQuestionsServlet?exam_id=${exam.examId}"><i class="icon-plus"></i> Add Questions</a>
											</c:otherwise>
										</c:choose>
									</td>
									<td>
										<c:choose>
											<c:when test="${exam.examType eq 'Real'}">
												<c:choose>
													<c:when test="${not exam.isLocked()}">
														<a class="btn btn-danger" href="DeactivateExamServlet?exam_id=${exam.examId }"><i class="icon-remove"></i> Deactivate</a>
													</c:when>
													<c:when test="${exam.isLocked()}">
														<a class="btn btn-info" href="ExamAddStudentsServlet?exam_id=${exam.examId }"><i class="icon-plus"></i> Add Students</a>
													</c:when>
												</c:choose>
											</c:when>
											<c:when test="${exam.examType eq 'Practice'}"><i class=" icon-ban-circle"></i> N/A</c:when>
										</c:choose>			
									</td>
									</tr>
								</c:forEach>
						</tbody>
					</table>
				</div>
			</div>

	
	<c:choose>
	<c:when test="${not empty message}">
	<script>$(document).ready(function() {$.noty({ text: '${message}', type: 'information', layout: 'top'});});</script>
	</c:when>
	<c:when test="${not empty error}">
	<script>$(document).ready(function() {$.noty({ text: '${error}',type: 'error',layout: 'top'});});</script>
	</c:when>
	<c:when test="${not empty success}">
	<script>$(document).ready(function() {$.noty({text: '${success}',type: 'success',layout: 'top'});});</script>
	</c:when>
	</c:choose>
	
	</div>
	</div>

	<script>
		$(document).ready(function() {
			$("button[name='createExam']").click(function() {
				$(".help-inline").remove();
				
				if($("input[name='examName']").val().length == 0) {
					$("input[name='examName']").after("<span class='help-inline'>Empty Field!</span>").parent().parent().addClass("error");
					return false;
				}
			});
		});
	</script>

</body>
</html>